package com.pxene.dmp.job;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pxene.dmp.common.DateUtils;
import com.pxene.dmp.common.ShellUtils;

public class JobManager {

	private static Log log = LogFactory.getLog(JobManager.class);
	
	private static final String DATA2HIVE_SHELL = "/data/user/shaoxiaoyu/automatic.sh";
	private static final String DATA2BASE_STATISTICS_SHELL = "/data/user/shaoxiaoyu/store-count.sh";
	

	public static void main(String[] args) {
		String date = args[0];
		if (!date.matches("^[\\d]{10}$")) {
			System.out.println("data format is incorrect!!!!!!!!!!!!!!!!");
			System.exit(1);
		}

		System.setProperty("date", date);
		log.info("<=ee-debug=>######" + date + " start######");
		String path = "/data/user/shaoxiaoyu/logs/";
		File file = new File(path + "telecomLog_" + date + ".log");
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(file, true));
			bw.write(
					"[START] " + DateUtils.getNowFormat("yyyy-MM-dd HH:mm:ss") + " [" + Thread.currentThread().getName()
							+ "] [" + JobManager.class.getName() + "] " + "######" + date + " start######" + "\n");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		ExecutorService pool = Executors.newFixedThreadPool(50);

		CountDownLatch latch = null;
		String hour = date.substring(8);

		if ("03".equals(hour)) {
			latch = new CountDownLatch(2);
//			new Thread(new UserInfoJob(pool, latch, bw)).start();
//			new Thread(new SearchWordJob(pool, latch, bw)).start();
			new Thread(new WeixinBizJob(pool, latch, bw)).start();
//			new Thread(new DomainInfoJob(pool, latch, bw)).start();
//			new Thread(new UserInfoFIXJob(pool, latch, bw)).start();
//			new Thread(new SearchWordFIXJob(pool, latch, bw)).start();
			new Thread(new WeixinBizFIXJob(pool, latch, bw)).start();
//			new Thread(new DomainInfoFIXJob(pool, latch, bw)).start();
		} else if ("04".equals(hour)) {
			latch = new CountDownLatch(2);
//			new Thread(new SearchWordJob(pool, latch, bw)).start();
//			new Thread(new DomainUserJob(pool, latch, bw)).start();
			new Thread(new WeixinBizJob(pool, latch, bw)).start();
//			new Thread(new DomainInfoJob(pool, latch, bw)).start();
//			new Thread(new SearchWordFIXJob(pool, latch, bw)).start();
//			new Thread(new DomainUserFIXJob(pool, latch, bw)).start();
			new Thread(new WeixinBizFIXJob(pool, latch, bw)).start();
//			new Thread(new DomainInfoFIXJob(pool, latch, bw)).start();
		} else {
			latch = new CountDownLatch(2);
//			new Thread(new SearchWordJob(pool, latch, bw)).start();
			new Thread(new WeixinBizJob(pool, latch, bw)).start();
//			new Thread(new DomainInfoJob(pool, latch, bw)).start();
//			new Thread(new SearchWordFIXJob(pool, latch, bw)).start();
			new Thread(new WeixinBizFIXJob(pool, latch, bw)).start();
//			new Thread(new DomainInfoFIXJob(pool, latch, bw)).start();
		}
		try {
			latch.await();
		} catch (InterruptedException e) {
			log.error("<=ee-debug=>JobLatch await Exception", e);
			try {
				bw.write("[ERROR] " + DateUtils.getNowFormat("yyyy-MM-dd HH:mm:ss") + " ["
						+ Thread.currentThread().getName() + "] [" + JobManager.class.getName() + "] " + "<=ee-debug=>"
						+ "JobLatch await Exception" + e + "\n");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		pool.shutdown();

		log.info("<=ee-debug=>######" + date + " end######");
		try {
			bw.write("[END] " + DateUtils.getNowFormat("yyyy-MM-dd HH:mm:ss") + " [" + Thread.currentThread().getName()
					+ "] [" + JobManager.class.getName() + "] " + "######" + date + " end######" + "\n");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		// 调用shell脚本入hive
		ShellUtils.executeShell(DATA2HIVE_SHELL + " " + date);
		// 发送信号触发
		ShellUtils.executeShell(DATA2BASE_STATISTICS_SHELL + " " + date);
	}

}
